<template>
    <div>
        <el-dialog
                title="添加新区域"
                :visible.sync="saveNewAreaShow"
                :show-close="false"
                :before-close="handleClose">
            <el-form label-position="right" :rules="rules" :model="areaForm" ref="ruleForm">
                <el-form-item label="区域名称" prop="name">
                    <el-input v-model="areaForm.name"></el-input>
                </el-form-item>
                <el-form-item label="人数上限" prop="limitNum">
                    <el-input-number size="mini" v-model.number="areaForm.limitNum"
                                     :min="1" label="人数上限"></el-input-number>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
    <el-button @click="cancel">取 消</el-button>
    <el-button type="primary" @click="submit" plain>确 定</el-button>
  </span>
        </el-dialog>
    </div>
</template>

<script>
    import {SaveNewArea} from '@/utils/apis/area'

    export default {
        name: "NewArea",
        data() {
            return {
                areaForm: {
                    name: '',
                    limitNum: 30
                },
                rules: {
                    name: [
                        {required: true, message: '请输入区域名称', trigger: 'blur'},
                        {min: 2, max: 8, message: '长度在 2 到 8 个字符', trigger: 'blur'}
                    ],
                }
            }
        },
        props: ['saveNewAreaShow'],
        methods: {
            handleClose() {
                this.$emit('cancel')
            },
            cancel() {
                //关闭本次表单验证提示框
                this.$refs['ruleForm'].clearValidate()
                this.$notify.info('取消添加')
                this.$emit('cancel')
            },
            //提交添加表单
            submit() {
                this.$refs['ruleForm'].validate(valid => {
                    if (valid) {
                        SaveNewArea(this.areaForm).then(res => {
                            console.log(res.data)
                            if (res.data.code === 200) {
                                this.$notify.success('添加成功')
                                this.$emit('saveSuccess')
                            } else {
                                this.$notify.error(res.data.message)
                            }
                        })
                    }
                })
            },
        }
    }
</script>

<style scoped>

</style>